package com.hsy.crm.web.utils;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.hsy.crm.web.domain.entity.Dict;

import java.util.List;
import java.util.Map;

/**
 * 系统配置字典一次性加载
 */
public class DictUtils {

    private static Map<String,List<Dict>> DICT_MAP = Maps.newHashMap();

    public static void setDicts(List<Dict> dicts){
        for( Dict dict : dicts ){
            setDict(dict);
        }
    }

    public static void setDicts(String customerId,List<Dict> dicts){
        DICT_MAP.put(customerId,dicts);
    }

    public static long getDictValue(String customerId,long code){
        List<Dict> dicts = getCustomerDicts(customerId);
        for( Dict dict : dicts ){
            if( dict.getCode().longValue() == code ){
                return dict.getValue();
            }
        }
        return -1;
    }

    public static List<Dict> getCustomerDicts(String customerId){
        return DICT_MAP.get(customerId);
    }

    private static void setDict(Dict dict){
        String customerId = dict.getCustomerId();
        List<Dict> customerDicts = DICT_MAP.get(customerId);
        if( customerDicts == null ){
            customerDicts = Lists.newArrayList();
            customerDicts.add(dict);
            DICT_MAP.put(customerId,customerDicts);
        }else{
            customerDicts.add(dict);
        }
    }

}
